package top.hidoctor.auscultation.adapter;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.tang.net.NetClient;
import com.tang.util.AppUtil;
import com.tang.util.TimeUtil;

import java.util.List;

import top.hidoctor.auscultation.R;
import top.hidoctor.auscultation.bean.ArbitrationBean;
import top.hidoctor.auscultation.widget.Player;

public class ZhongcaiAdapter extends BaseQuickAdapter<ArbitrationBean.ResultBean> {
    private int currentPos = -1;
    private Player currentPlayer;

    public Player getCurrentPlayer() {
        return currentPlayer;
    }

    public ZhongcaiAdapter(int layoutResId, List<ArbitrationBean.ResultBean> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final ArbitrationBean.ResultBean item) {
        helper.setText(R.id.tv_status, item.getStatus() == 20 ? "仲裁完成" : "正在仲裁")
                .setTextColor(R.id.tv_status, item.getStatus() == 20 ? mContext.getResources().getColor(R.color.blue_main) : mContext.getResources().getColor(R.color.text_gray))
                .setText(R.id.tv_time, "创建时间：" + TimeUtil.formatTime(item.getCreatedTime(), TimeUtil.Y_M_D_H_M_S_24))
                .setText(R.id.tv_long, "00:00/" + (item.getSampleDetail() != null && item.getSampleDetail().getDuration() != 0 ? AppUtil.stringForTime2(item.getSampleDetail().getDuration()) : "00:00"));


        if (item.getSample() != null) {
            helper.setText(R.id.tv_title, !TextUtils.isEmpty(item.getSample().getNo()) ? item.getSample().getNo() : "")
                    .setText(R.id.tv_tiwei, "体位：" + (!TextUtils.isEmpty(item.getSample().getPosition()) ? item.getSample().getPosition() : ""))
                    .setText(R.id.tv_peihe, "配合程度：" + (!TextUtils.isEmpty(item.getSample().getCoordinate()) ? item.getSample().getCoordinate() : ""));
        }

        helper.setOnClickListener(R.id.iv_bofang, new OnItemChildClickListener());

        if (item.getPlayStatus() == 0 || item.getPlayStatus() == 2) {
            Glide.with(mContext).load(R.mipmap.ic_zanting).into((ImageView) helper.getView(R.id.iv_bofang));
        } else if (item.getPlayStatus() == 1) {
            Glide.with(mContext).load(R.mipmap.ic_bofang).into((ImageView) helper.getView(R.id.iv_bofang));
        }

        final SeekBar seekbar = helper.getView(R.id.seekBar);
        //进度
        final Player player = new Player(seekbar);
        player.setOnplayerListener(new Player.OnplayerListener() {
            @Override
            public void onCompletion() {
                item.setPlayStatus(0);
                Glide.with(mContext).load(R.mipmap.ic_zanting).into((ImageView) helper.getView(R.id.iv_bofang));
                helper.setText(R.id.tv_long, "00:00/" + AppUtil.stringForTime2(item.getSampleDetail().getDuration()));
            }
        });

        seekbar.setProgress(item.getProgress());
        Glide.with(mContext).load((item.getPlayStatus() == 0 || item.getPlayStatus() == 2) ? R.mipmap.ic_zanting : R.mipmap.ic_bofang).into((ImageView) helper.getView(R.id.iv_bofang));
        helper.setOnClickListener(R.id.iv_bofang, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (item.getSampleDetail() == null || TextUtils.isEmpty(item.getSampleDetail().getWavPath())) {
                    Toast.makeText(mContext, "有损数据", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (currentPos != -1 && currentPos != helper.getAdapterPosition()) {
                    currentPlayer.stop();
                    getData().get(currentPos).setPlayStatus(0);
                    getData().get(currentPos).setProgress(0);
                    notifyItemChanged(currentPos);
                }
                currentPlayer = player;
                currentPos = helper.getAdapterPosition();

                if (item.getPlayStatus() == 0) { // 播放状态
                    item.setPlayStatus(1);
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            player.playUrl(NetClient.IMG_URL + item.getSampleDetail().getWavPath());

                        }
                    }).start();
                    Glide.with(mContext).load(R.mipmap.ic_bofang).into((ImageView) helper.getView(R.id.iv_bofang));
                } else if (item.getPlayStatus() == 1) {// 暂停状态
                    item.setPlayStatus(2);
                    if (player != null) {
                        player.pause();
                    }
                    Glide.with(mContext).load(R.mipmap.ic_zanting).into((ImageView) helper.getView(R.id.iv_bofang));
                } else if (item.getPlayStatus() == 2) { //继续播放
                    item.setPlayStatus(1);
                    if (player != null) {
                        player.play();
                    }
                    Glide.with(mContext).load(R.mipmap.ic_bofang).into((ImageView) helper.getView(R.id.iv_bofang));
                }
            }
        });
        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                final int currentPosition = player.getCurrentPosition();
                helper.setText(R.id.tv_long, AppUtil.stringForTime2(currentPosition) + "/" + AppUtil.stringForTime2(item.getSampleDetail().getDuration()));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }
}
